<script setup>
import {
    Edit,
    Delete,
    Share
} from '@element-plus/icons-vue'

import { ref } from 'vue'
import { ElNotification } from 'element-plus'

//文章分类数据模型
const categorys = ref([
    {
        "categoryId": 1,
        "categoryName": "热点123",
        "userId": 6,
        "updateTime": "2024-06-04 14:12:30"
    },
    {
        "categoryId": 1,
        "categoryName": "热点123",
        "userId": 6,
        "updateTime": "2024-06-04 14:12:30"
    },
    {
        "categoryId": 1,
        "categoryName": "热点123",
        "userId": 6,
        "updateTime": "2024-06-04 14:12:30"
    }
])

//用户搜索时选中的分类id
const categoryId = ref('')

//用户搜索时选中的发布状态
const state = ref('')

//文章列表数据模型
const articles = ref([
    {
        "articleId": 5,
        "title": "20人拔河比赛",
        "content": "兵马俑,华清池,法门寺,华山...爱去哪去哪...",
        "categoryId": "团体",
        "authorId": 8,
        "status": "草稿",
        "createTime": "2023-09-03 11:55:30",
        "updateTime": "2023-09-03 11:55:30"
    },
    {
        "articleId": 5,
        "title": "200米冲刺",
        "content": "兵马俑,华清池,法门寺,华山...爱去哪去哪...",
        "categoryId": "https://big-event-gwd.oss-cn-beijing.aliyuncs.com/9bf1cf5b-1420-4c1b-91ad-e0f4631cbed4.png",
        "authorId": "单人",
        "status": 2,
        "createTime": "2023-09-03 11:55:30",
        "updateTime": "2023-09-03 11:55:30"
    },
    {
        "articleId": 5,
        "title": "铅球",
        "content": "兵马俑,华清池,法门寺,华山...爱去哪去哪...",
        "categoryId": "https://big-event-gwd.oss-cn-beijing.aliyuncs.com/9bf1cf5b-1420-4c1b-91ad-e0f4631cbed4.png",
        "authorId": "单人",
        "status": 2,
        "createTime": "2023-09-03 11:55:30",
        "updateTime": "2023-09-03 11:55:30"
    },
])

//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(3)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    pageSize.value = size
    articleList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    pageNum.value = num
    articleList()
}










//添加文章
import { Plus } from '@element-plus/icons-vue'
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const articleModel = ref({
    title: '',
    content: '',
    categoryId: null,
    authorId: null,
    status: ''
})

//导入富文本的组件和样式
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'


//添加文章
import { ElMessage } from 'element-plus';





//定义变量控制标题的展示
const title = ref('')

//修改文章
const showDialog=(row)=>{
    visibleDrawer.value=true;
    title.value='编辑文章'
    //数据拷贝
    articleModel.value.title=row.title;
    articleModel.value.content=row.content;
    articleModel.value.categoryId=row.categoryId;
    articleModel.value.authorId=row.authorId;
    //扩展id属性，传递给后台，完成文章的修改
    articleModel.value.articleId=row.articleId
}


//编辑文章函数
const updateContent=async()=>{
    let result=await updateArticleContentService(articleModel.value);
    ElMessage.success('修改成功');
    //调用获取该用户文章函数
    articleList();
    visibleDrawer.value=false;
}

//清空模型中的数据
const clearData=()=>{
    // articleModel.value='';
    articleModel.value.title=''
    articleModel.value.content=''
    articleModel.value.categoryId=''
    articleModel.value.authorId=''
}

//消息提示函数
const successMsg = () => {
  ElNotification({
    title: '提示',
    message: '操作成功',
    type: 'success',
    duration: 1000,
  })
}




</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>项目管理</span>
                <div class="extra">
                    <el-button type="primary" @click="visibleDrawer = true; title='添加文章';clearData()">添加项目</el-button>
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form inline>
            <el-form-item label="项目分类：">
                <el-select style="width: 200px;" placeholder="请选择" v-model="categoryId">
                    <el-option v-for="c in categorys" :key="c.categoryId" :label="c.categoryName" :value="c.categoryId">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="项目状态：">
                <el-select style="width: 200px;" placeholder="请选择" v-model="state">
                    <el-option label="已通过" value="已通过"></el-option>
                    <el-option label="审核中" value="审核中"></el-option>
                    <el-option label="未通过" value="未通过"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="articleList">搜索</el-button>
                <el-button type="warning" @click="categoryId = ''; state = ''">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 文章列表 -->
        <el-table :data="articles" style="width: 100%">
            <el-table-column label="项目名称" width="400" prop="title"></el-table-column>
            <el-table-column label="项目分类" prop="authorId"></el-table-column>
            <el-table-column label="举行时间" prop="createTime"> </el-table-column>
            <!-- <el-table-column label="状态" prop="status">
                <template #default="{ row }"> -->
                    <!-- 根据状态值来设置不同的类或样式 -->
                    <!-- <span
                        :class="{ 'status-active': row.status === '已通过', 'status-inactive': row.status === '审核中', 'status-reject': row.status === '未通过' }">
                        {{ row.status }}
                    </span>
                </template>
            </el-table-column> -->
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteArticle(row.articleId);successMsg()"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5, 10, 15]"
            layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
            @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />





        <!-- 抽屉 -->
        <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
            <!-- 添加文章表单 -->
            <el-form :model="articleModel" label-width="100px">
                <el-form-item label="文章标题">
                    <el-input v-model="articleModel.title" placeholder="请输入标题"></el-input>
                </el-form-item>
                <el-form-item label="文章分类">
                    <el-select placeholder="请选择" v-model="articleModel.categoryId">
                        <el-option v-for="c in categorys" :key="c.categoryId" :label="c.categoryName" :value="c.categoryId">
                        </el-option>
                    </el-select>
                </el-form-item>
              
                <el-form-item label="文章内容">
                    <div class="editor">
                        <quill-editor theme="snow" v-model:content="articleModel.content" contentType="html">
                        </quill-editor>
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="title=='编辑文章'?updateContent():addArticle('审核中')">发布</el-button>
                    <!-- <el-button type="info">草稿</el-button> -->
                </el-form-item>
            </el-form>
        </el-drawer>
    </el-card>
</template>
<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    /* 定义不同状态的颜色样式 */
    .status-active {
        color: rgb(92, 243, 92);
        /* 文章通过 */
    }

    .status-inactive {
        color: rgb(247, 193, 75);
        /* 文章审核中 */
    }

    .status-reject {
        color: rgb(247, 87, 75);
        /* 文章未通过 */
    }

    /* 抽屉样式 */
    .avatar-uploader {
        :deep() {
            .avatar {
                width: 178px;
                height: 178px;
                display: block;
            }

            .el-upload {
                border: 1px dashed var(--el-border-color);
                border-radius: 6px;
                cursor: pointer;
                position: relative;
                overflow: hidden;
                transition: var(--el-transition-duration-fast);
            }

            .el-upload:hover {
                border-color: var(--el-color-primary);
            }

            .el-icon.avatar-uploader-icon {
                font-size: 28px;
                color: #8c939d;
                width: 178px;
                height: 178px;
                text-align: center;
            }
        }
    }

    .editor {
        width: 100%;

        :deep(.ql-editor) {
            min-height: 200px;
        }
    }
}
</style>